package by.alex.dzeshko.lastfmsimple.bo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import by.alex.dzeshko.lastfmsimple.bo.common.JSONModel;

public class AlbumModel extends JSONModel {

	private static final String TRACK = "track";

	private static final String TRACKS = "tracks";

	private static final String SUMMARY = "summary";

	private static final String WIKI = "wiki";

	private static final String RELEASEDATE = "releasedate";

	private static final String RESULTS = "results";

	private static final String ID = "id";

	private static final String ARTIST = "artist";

	private static final String ALBUM = "album";

	private static final String ALBUMMATCHES = "albummatches";

	private static final String LOG_TAG = "AlbumModel";

	public AlbumModel() {
		super();
	}

	public AlbumModel(JSONObject jsonObject) {
		super(jsonObject);
	}

	public AlbumModel(String jsonString) throws JSONException {
		super(jsonString);
	}

	public static List<JSONObject> getAlbumsList(Object source)
			throws JSONException {
		return getObjectsList(source, ALBUMMATCHES, ALBUM);
	}

	@Override
	public String getName() throws JSONException {
		return super.getName();
	}

	@Override
	public String getMbid() throws JSONException {
		return super.getMbid();
	}

	@Override
	public String getUrl() throws JSONException {
		return super.getUrl();
	}

	public String getArtist() throws JSONException {
		return getString(ARTIST);
	}

	public int getId() throws JSONException {
		return getInt(ID);
	}

	public String getReleaseDate() throws JSONException {
		return getString(RELEASEDATE);
	}

	public String getSummary() throws JSONException {
		return getStringFromObject(WIKI, SUMMARY);
	}

	public static List<JSONObject> getTracks(JSONObject source) throws JSONException {
		if(source == null) {
			throw new IllegalArgumentException("String source is null");
		}
		JSONArray array = source.getJSONObject(ALBUM).getJSONObject(TRACKS).getJSONArray(TRACK);
		if(array.length()==0) {
			Log.d(LOG_TAG, "array size 0");
			return new ArrayList<JSONObject>();
		}
		List<JSONObject> list = new ArrayList<JSONObject>(array.length());
		for (int i = 0; i < array.length(); i++) {
			JSONObject track = array.getJSONObject(i);
			JSONObject artist = track.getJSONObject(ARTIST);
			String artistname = artist.getString(NAME);
			track.put(ARTIST, artistname);
			list.add(track);
		}
		return list;
	}

	@Override
	public String getImage(int size) throws JSONException {
		return super.getImage(size);
	}

}
